import geom.FacetList;
import geom.LineSet;
import geom.PolygonList;
import geom.Solid;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.media.j3d.BoundingBox;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.vecmath.Point3d;

import ui.PolygonListRenderer;

public class SliceDisplay extends JFrame implements AdjustmentListener
{
	public static void main(String[] args) throws Exception
	{
		StlFile loader = new StlFile();
		Solid scene = loader.load("r.stl");
		BoundingBox bounds = (BoundingBox) scene.getBounds();
		
		Point3d lower = new Point3d(), upper = new Point3d();
		
		bounds.getLower(lower);
		bounds.getUpper(upper);
		
		FacetList fl = loader.generateFacetList();
		
		new SliceDisplay(fl, scene, lower.z, upper.z);
	}
	
	public SliceDisplay(FacetList facets, Solid solid, double minZ, double maxZ)
	{
		this.facetList = facets;
		this.minZ = minZ;
		this.maxZ = maxZ;	

		facetList.setLabel(polyLabel);

		plr = new PolygonListRenderer(solid);
		
		JScrollBar scroll = new JScrollBar(JScrollBar.VERTICAL);
		scroll.addAdjustmentListener(this);
		scroll.setMinimum(0);
		scroll.setMaximum(1000);
		scroll.setVisibleAmount(1);		

		JPanel container = new JPanel();
		container.setLayout(new BorderLayout());		
		container.add(plr, BorderLayout.CENTER);

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setLeftComponent(new JScrollPane(segList));
		splitPane.setRightComponent(container);

		segList.setModel(new DefaultListModel());
		segList.setFont(new Font("Consolas", Font.PLAIN, 12));

		plr.setPreferredSize(new Dimension(600, 700));

		setLayout(new BorderLayout());

		getContentPane().add(splitPane, BorderLayout.CENTER);		
		getContentPane().add(scroll, BorderLayout.EAST);
		getContentPane().add(polyLabel, BorderLayout.SOUTH);		

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		pack();

		setVisible(true);
	}

	public void displaySlice(double z)
	{
		ArrayList<LineSet> linesets = facetList.getLineSetsForZ(z);

		if (linesets == null)
			return;

		DefaultListModel model = (DefaultListModel) segList.getModel();
		model.clear();
		for (LineSet set : linesets)
		{
			/*model.addElement(set.getStart());            
			 model.addElement(set.getEnd());*/
			Point2D old = null;
			for (Point2D pts : set.getPoints())
			{
				if (pts.equals(set.getStart()) || pts.equals(set.getEnd()))
					model.addElement(pts);
				else if (old != null && !pts.equals(old))
					model.addElement("  " + pts);

				old = pts;
			}
			model.addElement(set.numVertices() + " " + set.getLength());
			model.addElement(" ");
		}

		PolygonList slices = facetList.getSliceForZ(linesets);
		
		System.out.println(slices.size());
		
		plr.setPolygonList(slices);
	}

	FacetList facetList;
	PolygonListRenderer plr;
	double minZ, maxZ;

	public void adjustmentValueChanged(AdjustmentEvent e)
	{
		int depthPercentage = e.getValue();

		double zVal = minZ + ((maxZ - minZ) / 999 * depthPercentage);

		displaySlice(zVal);
	}

	private JLabel polyLabel = new JLabel();
	private JList segList = new JList(new DefaultListModel());
}
